﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Exdot
{
    public class Plan
    {
        IOLog log = new IOLog();
        QuanLyChiTieuEntities context = new QuanLyChiTieuEntities();
        public object keHoachShow()
        {
            object kq = new object();
            var query1 = from kh in context.KeHoachChiTieux select kh;
            var query = new[] { new KeHoachChiTieu() }.Concat(query1);
            kq = query.ToList();
            return kq;
        }
        public String keHoachInsert(KeHoachChiTieu a)
        {
            String s = "";
            try
            {
                context = new QuanLyChiTieuEntities();
                context.KeHoachChiTieux.AddObject(a);
                context.SaveChanges();
                s = "Thêm kế hoạch thành công";
            }
            catch(Exception e)
            {
                s = "Thêm kế hoạch thất bại "+e.Message;
                log.WriteFile(s);
            }
            return s;
        }
        public String keHoachUpdate(KeHoachChiTieu a)
        {
            String s = "";
            try
            {
                KeHoachChiTieu kehoach = context.KeHoachChiTieux.Single(p => p.MaKeHoach == a.MaKeHoach);
                kehoach.TenKeHoach = a.TenKeHoach;
                kehoach.MoTa = a.MoTa;
                kehoach.ConLai = a.ConLai;
                kehoach.ThoiGian = a.ThoiGian;
                kehoach.SoTien = a.SoTien;
                kehoach.TrangThai = a.TrangThai;
                context.SaveChanges();
                s = "Cập nhật thành công";
            }
            catch (Exception e)
            {
                s = "Sửa kế hoạch thất bại " + e.Message;
                log.WriteFile(s);
            }
            return s;
        }
        public string keHoachDelete(KeHoachChiTieu a)
        {
            String s = "";
            try
            {
                context = new QuanLyChiTieuEntities();
                KeHoachChiTieu kehoach = context.KeHoachChiTieux.Single(p => p.MaKeHoach == a.MaKeHoach);
                context.DeleteObject(kehoach);
                context.SaveChanges();
                s = "Xóa thành công";
            }
            catch(Exception e)
            {
                s = "Thêm kế hoạch thất bại " + e.Message;
                log.WriteFile(s);
            }
            return s;
        }
        public String[] thongbao(ref List<KeHoachChiTieu> khct)
        {
            object kq = new object();
            var query1 = from kh in context.KeHoachChiTieux select kh;
            String[] s = new String[query1.ToList().Count];
            int i = 0;
            foreach (KeHoachChiTieu k in query1.ToList())
            {
                if (DateTime.Now.Day == k.ThoiGian.Day && DateTime.Now.Month == k.ThoiGian.Month && DateTime.Now.Year == k.ThoiGian.Year && k.TrangThai.Trim() == "Active")
                {
                    s[i] = k.ThoiGian + "\n" + k.TenKeHoach + ":" + k.SoTien;
                    k.TrangThai = "Finish";
                    keHoachUpdate(k);
                    khct.Add(k);
                }
                i++;
            }
            return s;
        }
    }
}
